/*
 * @(#) $Header$
 *
 * Copyright (C)  2011  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util;

import java.util.Comparator;

import ca.forklabs.baselib.util.comparator.ChainComparator;
import ca.forklabs.baselib.util.comparator.ComparableComparator;
import ca.forklabs.baselib.util.comparator.ReverseComparator;

/**
 * Class {@code Comparators} provides factory methods for {@link Comparator}s.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.Comparators">Daniel Léonard</a>
 * @version $Revision$
 */
public class Comparators {

//---------------------------
// Constructor
//---------------------------

   /**
    * Let no one instantiate this class.
    */
   protected Comparators() {
   // nothing
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Creates a new comparator for the given comparable class.
    * @param   <C>   the comparable class.
    * @return   a new comparator.
    */
   @SuppressWarnings("rawtypes")
   public static <C extends Comparable> Comparator<C> comparator() {
      Comparator<C> comparator = ComparableComparator.create();
      return comparator;
      }

   /**
    * Creates a new reverse comparator.
    * @param   <T>   the type
    * @param   comparator   the adapted comparator.
    * @return   a  new reverse comparator.
    */
   public static <T> Comparator<T> reverse(Comparator<T> comparator) {
      Comparator<T> reverse = ReverseComparator.create(comparator);
      return reverse;
      }

   /**
    * Creates a new chain comparator.
    * @param   <T>   the type
    * @param   comparators   the comparators forming the chain.
    * @return   a  new chain comparator.
    */
   @SafeVarargs
   public static <T> Comparator<T> chain(Comparator<T>... comparators) {
      Comparator<T> comparator = chain(Arrays.iterable(comparators));
      return comparator;
      }

   /**
    * Creates a new chain comparator.
    * @param   <T>   the type
    * @param   comparators   the comparators forming the chain.
    * @return   a  new chain comparator.
    */
   public static <T> Comparator<T> chain(Iterable<Comparator<T>> comparators) {
      Comparator<T> comparator = ChainComparator.create(comparators);
      return comparator;
      }

   }
